/*
============================================================================
 Name        : BlockBKLX.cpp
 Author      : 
 Version     :
 Copyright   : Your copyright notice
============================================================================
*/

#include "BlockBKLX.h"
#include "BlockGrid.h"
#include "eStockappui.h"
#include "Field.h"
#include <eStock_L2HY.rsg>

CBlockBKLX::CBlockBKLX(CBaseBlock* bsBack):
CBlockGoods(bsBack),
m_nCurrent(0)
{
}

CBlockBKLX::~CBlockBKLX()
{
	iItemArray.Close();
	iShowArray.Close();
}

CBlockBKLX* CBlockBKLX::NewLC(CBaseBlock* bsBack)
{
	CBlockBKLX* self = new (ELeave)CBlockBKLX(bsBack);
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
}

CBlockBKLX* CBlockBKLX::NewL(CBaseBlock* bsBack)
{
	CBlockBKLX* self=CBlockBKLX::NewLC(bsBack);
	CleanupStack::Pop(); // self;
	return self;
}

void CBlockBKLX::ConstructL()
{
	CBlockGoods::ConstructL();
	m_bCanSetGoods = ETrue;
	CGlobal::GetDesFromID(m_strOK,LOC_BUF_MENU_MENU);
	CGlobal::GetDesFromID(m_strTitle, LOC_BUF_MENU_BKLX);
	m_sbVert->m_bScroll2End = ETrue;
	SetScrollBar(0, 0);
}

void CBlockBKLX::OnDraw(CFbsBitGc& g) const
{
	TField field;
	field.m_nGoodsID = m_goods.m_nGoodsID;
	field.m_nClose = m_goods.m_nClose;

	if(!m_bSocketed)
	{
		TBuf<32> bufDownloading;
		CGlobal::GetDesFromID(bufDownloading,LOC_BUF_STR_DOWNLOADING);//"\xD5\xFD\xD4\xDA\xCF\xC2\xD4\xD8\xD0\xC5\x{03e2}, \xC7\xEB\xC9\x{0535}\xC8..."
		CGlobal::DrawString(g,bufDownloading,0,m_nLineHeight,TOP|LEFT,m_pGlobal->g_rgbText);
		return;
	}

	TInt nSize = iShowArray.Count();
	if (nSize == 0)
	{
		TBuf<32> noInfo;
		CGlobal::GetDesFromID(noInfo,LOC_BUF_STR_NOINFO);//"\xD5\xFD\xD4\xDA\xCF\xC2\xD4\xD8\xD0\xC5\x{03e2}, \xC7\xEB\xC9\x{0535}\xC8..."
		CGlobal::DrawString(g,noInfo,0,m_nLineHeight,TOP|LEFT,m_pGlobal->g_rgbText);
		return;
	}
	TInt nStart = m_nCurrent/m_nLinesPerPage*m_nLinesPerPage;

	for (TInt i = 0; i<m_nLinesPerPage; i++)
	{
		TInt nPos = nStart+i;
		if (nPos>=nSize)
			break;

		TInt nY = m_rect.iTl.iY + m_nLineHeight*i;
		TItem item = iShowArray[nPos];
		TRgb rgb = m_pGlobal->g_rgbText;
		TRgb smallRectColor(m_pGlobal->g_rgbRise);
		TRect rect(TPoint(m_rect.iTl.iX, nY), TSize(m_rect.Width(), m_nLineHeight));
		TRect smallRect(TPoint(MarginSpace, rect.iBr.iY - (rect.Height() + (MarginSpace / 2)) / 2),
			TSize(MarginSpace / 2, MarginSpace / 2));

		g.SetPenStyle(CGraphicsContext::ESolidPen);

		if (m_nCurrent == nPos)
		{
			rgb = m_pGlobal->g_rgbHighlight;
			DrawImageRect(g, rect, 1);
		}
		if (item.iType == TItem::BIG)
		{
			rgb = m_pGlobal->g_rgbHighlight;
			g.SetPenColor(smallRectColor);
			g.SetBrushColor(smallRectColor);
			g.DrawRect(smallRect);
			g.SetPenStyle(CGraphicsContext::EDottedPen);
			if (item.iId == 15)
				g.DrawLine(TPoint(MarginSpace + smallRect.Width()/2, smallRect.iTl.iY), TPoint(MarginSpace + smallRect.Width()/2, rect.iBr.iY)); 
			else if (!item.iIsSystem)
				g.DrawLine(TPoint(MarginSpace + smallRect.Width()/2, rect.iTl.iY), TPoint(MarginSpace + smallRect.Width()/2, rect.iBr.iY)); 
			else
				g.DrawLine(TPoint(MarginSpace + smallRect.Width()/2, rect.iTl.iY), TPoint(MarginSpace + smallRect.Width()/2, smallRect.iBr.iY));
			g.DrawLine(TPoint(MarginSpace*3, rect.iTl.iY + rect.Height() / 2), TPoint(MarginSpace*3, rect.iBr.iY));
			g.DrawLine(TPoint(MarginSpace*2, rect.iTl.iY + rect.Height() / 2), TPoint(MarginSpace*4, rect.iTl.iY + rect.Height() / 2));

			CGlobal::DrawString(g, item.iName, MarginSpace*4, nY, TOP|LEFT, rgb);
		} 
		else if (item.iType == TItem::SMALL)
		{
			g.SetPenStyle(CGraphicsContext::EDottedPen);
			g.SetPenColor(smallRectColor);
			if (nPos != nSize -1)
			{
				if (iShowArray[nPos + 1].iType == TItem::SMALL)
					g.DrawLine(TPoint(MarginSpace*3, rect.iTl.iY), TPoint(MarginSpace*3, rect.iBr.iY));
				else if (iShowArray[nPos + 1].iType == TItem::BIG)
					g.DrawLine(TPoint(MarginSpace*3, rect.iTl.iY), TPoint(MarginSpace*3, rect.iBr.iY - m_nLineHeight / 2));
			}
			else
			{
				g.DrawLine(TPoint(MarginSpace*3, rect.iTl.iY), TPoint(MarginSpace*3, rect.iBr.iY - m_nLineHeight / 2));
			}
			if (!item.iIsSystem)
				g.DrawLine(TPoint(MarginSpace + smallRect.Width()/2, rect.iTl.iY), TPoint(MarginSpace + smallRect.Width()/2, rect.iBr.iY));
			g.DrawLine(TPoint(MarginSpace*3, rect.iBr.iY - m_nLineHeight / 2), TPoint(MarginSpace*6, rect.iBr.iY - m_nLineHeight / 2));
			g.SetPenStyle(CGraphicsContext::ESolidPen);

			CGlobal::DrawString(g, item.iName, MarginSpace*6, nY, TOP|LEFT, rgb);
		}
	}
	g.SetPenStyle(CGraphicsContext::ESolidPen);
}

void CBlockBKLX::OnKeyDown(TInt keyCode)
{
	if (keyCode == m_pGlobal->g_nKeyUp)
	{
		if (iShowArray.Count() > 0)
		{
			m_nCurrent--;
			if (m_nCurrent < 0)
			{
				m_nCurrent = 0;
				iTabbar->SetFocus(ETrue, m_pGlobal->g_nKeyUp);
				DrawNow();
				return;
			}
			if (m_bShowScroll)
				m_sbVert->SetPos(m_nCurrent);
		}
		else
		{
		    iTabbar->SetFocus(ETrue, m_pGlobal->g_nKeyUp);
			DrawNow();
			return;
		}
	} 
	else if (keyCode==m_pGlobal->g_nKeyDown)
	{
		if (iShowArray.Count() > 0)
		{
			m_nCurrent++;
			if (m_nCurrent == iShowArray.Count())
			{
				m_nCurrent = iShowArray.Count() - 1;
				iToolbar->SetFocus(ETrue);
				DrawNow();
				return;
			}
			if (m_bShowScroll)
				m_sbVert->SetPos(m_nCurrent);
		}
		else
		{
			iToolbar->SetFocus(ETrue);
			DrawNow();
			return;
		}
	}
	else if (keyCode == m_pGlobal->g_nKeySelect)
	{
		if (iShowArray.Count() > 0)
		{
			TItem item = iShowArray[m_nCurrent];
			if (item.iType == TItem::SMALL)
			{
				CBlockGrid* grid = NULL;
				if (item.iIsSystem)
					grid = CBlockGrid::NewL(NULL, item.iId, item.iName, ZDF);
				else
					grid = CBlockGrid::NewL(NULL, -(item.iId), item.iName, ZDF);
				//grid->InitData();
				GetContainer()->AddBlock(grid);
			}
		}
	}
	else
	{
		CBlockGoods::OnKeyDown(keyCode);
	}

	DrawNow();
}

void CBlockBKLX::OnMouseUp(const TPoint& aPt)
{
	TInt nPos = aPt.iY/m_nLineHeight-1;
	if (nPos>=0 && nPos<m_nLinesPerPage)
	{
		nPos += m_nCurrent/m_nLinesPerPage*m_nLinesPerPage;

		TInt nSize = iShowArray.Count();
		if (nPos<nSize)
		{
			if (m_nCurrent!=nPos)
			{
				m_nCurrent = nPos;
				if (m_bShowScroll)
					m_sbVert->SetPos(m_nCurrent);
				DrawNow();
			}
			else
			{
				OnKeyEvent(m_pGlobal->g_nKeySelect);
			}
		}
	}
}

void CBlockBKLX::ChangeScrollPos(TInt nPos)
{
	if (m_nCurrent!=nPos)
	{
		m_nCurrent = nPos;
		DrawNow();
	}
}

void CBlockBKLX::SizeChanged()
{
	CBlockGoods::SizeChanged();
	iTitlebar->FocusEnabled(ETrue);
	SetScrollBar(m_nCurrent, iShowArray.Count());
}

TInt CBlockBKLX::GetDataLength()
{
	return 4;
}

TInt16 CBlockBKLX::GetDataType()
{
	return RequestDataType_BKLX;
}

TBool CBlockBKLX::ReadData(const TDesC8& aData)
{
	TPtrC8 ptr(aData);
	m_bSocketed = ETrue;
	m_bExit = ETrue; //\xB1\xBE\xC0\xE0\x{05bb}\xCA\xD5\x{04bb}\xB1\xE9\xCA\xFD\xBE\xDD

	iItemArray.Reset();
	iShowArray.Reset();
	TInt8 bStatus = CGlobal::ReadInt8L(ptr);
	if ((bStatus & 2) != 0)//\x{03f5}\x{0373}\xB0\xE5\xBF\x{98ec}\xB2\xBB\x{036c}\xCF\xC2\xC3\xE6\xB5\xC4\xCA\xC7\xC4\x{06b2}\xBF\xD7\x{04f0}\xE5\xBF\xE9\x{04b2}\xD3\xD0id\xA3\xAC\xD3\xC3SystemBKid\xBC\xC7\x{00bc}
	{
		TInt16 num = CGlobal::ReadInt16L(ptr);
		TInt16 systemId = 0;//CGlobal::ReadInt16L(ptr);
		HBufC* systemName = CGlobal::ReadStringL(ptr);
		TItem item(TItem::BIG, 0, systemName->Des(), ETrue);
		iItemArray.Append(item);
		delete systemName;
		for (TInt i = 0; i < num; ++i)
		{
			systemId = CGlobal::ReadInt16L(ptr);
			systemName = CGlobal::ReadStringL(ptr);
			TItem item(TItem::SMALL, systemId, systemName->Des(), ETrue);
			iItemArray.Append(item);
			delete systemName;
		}
	}
	if (bStatus != 0)
	{
		TInt8 bignum = CGlobal::ReadInt16L(ptr);//\xB4\xF3\xC0\xE0
		for(TInt i = 0; i < bignum; ++i)
		{
			TInt16 bigId = CGlobal::ReadInt16L(ptr);
			HBufC* bigName = CGlobal::ReadStringL(ptr);
			TItem item(TItem::BIG, bigId, bigName->Des());
			iItemArray.Append(item);
			delete bigName;

			TInt16 smallnum = CGlobal::ReadInt16L(ptr);
			for (TInt j = 0; j < smallnum; ++j)
			{
				HBufC* smallName = CGlobal::ReadStringL(ptr);
				TItem item(TItem::SMALL, bigId, smallName->Des());
				iItemArray.Append(item);
				delete smallName;
			}
		}
	}

	if (iItemArray.Count() > 0)//\xB5\xF7\xD5\xFB\x{02f3}\xD0\xF2,\xCF\xD4\x{02be}\x{02f3}\xD0\xF2\x{03aa}\xD0\xD0\x{04b5}\xA3\xAC\xB8\xC5\xC4\x{e8ec}\xB5\xD8\xC7\xF8\xA3\xAC\x{03f5}\x{0373}
	{
		for (TInt i = 0; i < iItemArray.Count(); ++i)
			if (iItemArray[i].iId == 15)//\xD0\xD0\x{04b5}
				iShowArray.Append(iItemArray[i]);
		for (TInt i = 0; i < iItemArray.Count(); ++i)
			if (iItemArray[i].iId == 16)//\xB5\xD8\xC7\xF8
				iShowArray.Append(iItemArray[i]);
		for (TInt i = 0; i < iItemArray.Count(); ++i)
			if (iItemArray[i].iId == 14)//\xB8\xC5\xC4\xEE
				iShowArray.Append(iItemArray[i]);
		for (TInt i = 0; i < iItemArray.Count(); ++i)
			if (iItemArray[i].iId != 16 && iItemArray[i].iId != 15 && iItemArray[i].iId != 14)
				iShowArray.Append(iItemArray[i]);

		SetScrollBar(m_nCurrent, iShowArray.Count());
	}
	return ETrue;
}

void CBlockBKLX::WriteData(TDes8& aBuf)
{
	CGlobal::WriteInt32L(aBuf,m_goods.m_nGoodsID);
}	

